home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / b_queue.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  1.8 KB  |  88 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  b_queue.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_BQUEUE_H
  16. #define LEDA_BQUEUE_H
  17.  
  18. //------------------------------------------------------------------------------
  19. // bounded queues
  20. //------------------------------------------------------------------------------
  21.  
  22. #include <LEDA/basic.h>
  23.  
  24.  
  25. template<class type> 
  26.  
  27. class _CLASSTYPE b_queue  
  28. {
  29.         type* first;     // first element of array
  30.         type* stop;      // one position behind last element of array
  31.         type* start;     // current start of queue (wraps around)
  32.         type* end;       // one position behind end of queue (wraps around)
  33.  
  34. public:                                        
  35.  
  36. b_queue(int s) 
  37. {
  38. #if !defined(LEDA_CHECKING_OFF)
  39.   if (s<1) error_handler(88,"_b_queue: bad size");
  40. #endif
  41.   first = new type[s];
  42.   if (first==0) error_handler(88,"_b_queue: out of memory");
  43.   stop  = first+s;
  44.   start = end = first; 
  45. }
  46.  
  47. virtual ~b_queue() { delete [] first; }
  48.  
  49. int empty() const { return (size()==0) ? true : false; }
  50.  
  51. int size() const 
  52. { int s = end-start;
  53.   return (s<0) ?  (stop-first+s) : s;
  54. }
  55.  
  56. void append(type& a)
  57. { *end++ = a;
  58.   if (end == stop) end = first;
  59. #if !defined(LEDA_CHECKING_OFF)
  60.   if (start==end) error_handler(88, "_b_queue overflow");
  61. #endif
  62. }
  63.  
  64. type pop()
  65. {
  66. #if !defined(LEDA_CHECKING_OFF)
  67.  if (start==end) error_handler(88, "_b_queue underflow");
  68. #endif
  69.   type x = *start++;
  70.   if (start == stop) start = first;
  71.   return x;
  72. }
  73.  
  74. type top() const
  75. {
  76. #if !defined(LEDA_CHECKING_OFF)
  77.   if (start==end) error_handler(88, "_b_queue empty");
  78. #endif
  79.   return *start;
  80. }
  81.  
  82. void clear() { start = end = first; }
  83.  
  84. };
  85.  
  86. #endif
  87.